package com.cjh.mybatis08.session.defaults;


import com.cjh.mybatis08.executor.Executor;
import com.cjh.mybatis08.mapping.MappedStatement;
import com.cjh.mybatis08.session.Configuration;
import com.cjh.mybatis08.session.SqlSession;
import lombok.AllArgsConstructor;

import java.util.List;

/**
 * @Descripton
 * @Author cjh
 * @Date 2023/5/24 15:11
 */
@AllArgsConstructor
public class DefaultSqlSession implements SqlSession {
    /**
     * 映射器注册机
     */
    private Configuration configuration;
    private Executor executor;

    @Override
    public <T> T selectOne(String statement) {
        return this.selectOne(statement, null);
    }

    @Override
    public <T> T selectOne(String statement, Object parameter) {
        MappedStatement mappedStatement = configuration.getMappedStatement(statement);
        List<T> list = executor.query(mappedStatement, parameter, Executor.NO_RESULT_HANDLER,
                mappedStatement.getSqlSource().getBoundSql(parameter));
        return list.get(0);

    }


    @Override
    public Configuration getConfiguration() {
        return configuration;
    }

    @Override
    public <T> T getMapper(Class<T> type) {
        return configuration.getMapper(type, this);
    }
}
